package com.xgggh.redpack.ui.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.ViewModelProviders;

import com.jakewharton.rxbinding3.view.RxView;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.xgggh.redpack.R;
import com.xgggh.redpack.base.ui.BaseFragment;
import com.xgggh.redpack.databinding.CreateFragmentBinding;
import com.xgggh.redpack.databinding.HomeFragmentBinding;
import com.xgggh.redpack.databinding.RankFragmentBinding;
import com.xgggh.redpack.domain.app.Constant;
import com.xgggh.redpack.domain.auth.Auth;
import com.xgggh.redpack.domain.redpack.Pack;
import com.xgggh.redpack.ui.login.LoginActivity;
import com.xgggh.redpack.ui.redpack.CategoryActivity;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import dagger.android.support.AndroidSupportInjection;
import io.reactivex.functions.Consumer;
import io.reactivex.subscribers.DisposableSubscriber;
import kotlin.Unit;
import viewmodel.ProfileViewModel;

import static com.baidu.mapapi.BMapManager.getContext;

public class CreateFragment extends BaseFragment {
    @Inject
    Auth auth;
    CreateFragmentBinding mBinding;

    public static CreateFragment newInstance() {
        return new CreateFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.create_fragment, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.create, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mBinding.appBarHome.twinklingRefreshLayout.setPureScrollModeOn();
        RxView.clicks(mBinding.appBarHome.sendRedPackNow)
                .compose(getBaseActivity().uiTransformer())
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)))
                .subscribe(new Consumer<Unit>() {
                    @Override
                    public void accept(Unit unit) throws Exception {
                        redPackOrLogin();
                    }
                });
    }

    private void redPackOrLogin(){
        boolean _auth = TextUtils.isEmpty(auth.getAccessToken());
        if (_auth){
            startActivity(new Intent(getContext(), LoginActivity.class));
        }else{
            startActivity(new Intent(getContext(), CategoryActivity.class));
        }

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_question:
                Toast.makeText(getContext(), "为什么发红包解释", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        AndroidSupportInjection.inject(this);
        super.onAttach(context);
        //mViewModel = ViewModelProviders.of(this,factory).get(ProfileViewModel.class);
    }
}
